package com.tmpt.controller.springmvc.handlers;

import com.tmpt.controller.springmvc.PageUtils;
import com.tmpt.dto.A2UserDto;
import com.tmpt.dto.Result;
import com.tmpt.service.IOrgService;
import com.tmpt.service.ISecurityV2Service;
import com.tmpt.utils.MD5Util;
import com.tmpt.utils.StringUtils;
import com.tmpt.utils.WebUtils;
import com.tmptSource.dto.*;
import com.tmptSource.service.intf.IA2userService;
import com.tmptSource.service.intf.IOwNumberInfoService;
import com.tmptSource.service.intf.IOwOffpostRecordService;
import com.tmptSource.service.intf.IOwSigninService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by wxy on 2017/10/17.
 */

@Controller
@RequestMapping("/springmvc/user")
public class UserAction {

    private Logger log = Logger.getLogger(this.getClass());

    @Resource
    private ISecurityV2Service securityV2Service;
    @Resource
    private IOwNumberInfoService owNumberInfoService;
    @Resource
    private IOwSigninService owSigninService;
    @Resource
    private IOwOffpostRecordService owOffpostRecordService;

    @Resource private IOrgService orgService;

    @Resource private IA2userService a2userService;

    /**
     * 用户登录APP
     * @return
     */
    @RequestMapping(value = "/loginAPP")
    @ResponseBody
    public Result loginApp(String appLoginname,String appPassword, String clientID,  HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<A2UserDto> result = new Result();
        try {
            //验证用户账号与密码是否对应
            A2UserDto empDto = securityV2Service.getUserByAppLoginname(appLoginname);
            if(empDto == null){
                return result.fault("用户名不存在");
            }
            if (empDto.getDeleteFlag() == 1){
                return result.fault("用户状态不正常");
            }

            String psdMd5 = appPassword+appLoginname;
            psdMd5 = MD5Util.toMD5ForApp(psdMd5);

            if(!empDto.getPassword().toLowerCase().equals(psdMd5.toLowerCase())) {
                return result.fault("密码不正确");
            }

            long userId = empDto.getId();
            long companyId = empDto.getCompanyId();
            session.setAttribute("username", appLoginname);
            session.setAttribute("userFullName", empDto.getName());
            session.setAttribute("userId", String.valueOf(userId));
            session.setAttribute("companyId", String.valueOf(companyId));

            empDto.setPassword("");
            empDto.setAppPassword("");

            //更新clientID for push
            if(clientID!=null && clientID.length()>2) {
                securityV2Service.updateClientIDByUser("" + userId, clientID);
            }

            //init 岗位下级员工
            orgService.initPostUser(userId);

            result.setResultObj(empDto);
            return result.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 查看个人资料
     * @return
     */
    @RequestMapping(value = "/queryUserById")
    @ResponseBody
    public Result queryUserById(String userId ,HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<A2UserDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //验证用户账号与密码是否对应
            A2UserDto empDto = securityV2Service.queryUserById(userId);
            if(empDto == null){
                return result.fault("用户名不存在");
            }
            if (empDto.getDeleteFlag() == 1){
                return result.fault("用户资料已删除");
            }

            empDto.setPassword("");
            empDto.setAppPassword("");

            result.setResultObj(empDto);
            return result.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 查看用户状态
     * @return
     */
    @RequestMapping(value = "/queryUserStatusByIdsWithoutLogin")
    @ResponseBody
    public Result queryUserStatusByIds(String userIds ,HttpServletRequest request){
        Result result = new Result();
        try {

            if(!StringUtils.isNull(userIds)){
                String[] split = userIds.split(",");

            }

            //调用远程服务
            result = a2userService.queryUserStatusByIds(userIds);


        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
        return result;
    }




    /**
     *
     * @return
     */
    @RequestMapping(value = "/queryLowPostUsersByUser")
    @ResponseBody
    public Result queryLowPostUsersByUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        log.debug("====>queryLowPostUsersByUser" );

        Result<OwNumberInfoDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);

            long userIdLong = Long.parseLong(userId);

            //调用远程服务
            result =  orgService.queryLowPostUsersByUser(userIdLong);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 生成编号
     * @return
     */
    @RequestMapping(value = "/generateNumber")
    @ResponseBody
    public Result generateNumber(OwNumberInfoDto owNumberInfoDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwNumberInfoDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            owNumberInfoDto.setCreatemanId(userId);
            owNumberInfoDto.setCreateman(username);
            owNumberInfoDto.setCompanyId(companyId);

            //插入数据
            //owNumberInfoDto.setType(owNumberInfoDto.getType());

            //调用远程服务
            result =  owNumberInfoService.addData(owNumberInfoDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }

    @RequestMapping(value = "/checkAndAddSignin")
    @ResponseBody
    public Result checkAndAddSignin( OwSigninDto owSigninDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwSigninDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }

            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            owSigninDto.setCreatemanId(userId);
            owSigninDto.setCreateman(username);
            owSigninDto.setCompanyId(companyId);
            //插入数据
            owSigninDto.setA2userId(userId);

            //调用远程服务
            result =  owSigninService.checkAndAddSignin(owSigninDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }

    @RequestMapping(value = "/signin")
    @ResponseBody
    public Result signin( OwSigninDto owSigninDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwSigninDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }

            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            owSigninDto.setCreatemanId(userId);
            owSigninDto.setCreateman(username);
            owSigninDto.setCompanyId(companyId);
            //插入数据
            owSigninDto.setA2userId(userId);

            //调用远程服务
            result =  owSigninService.addData(owSigninDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    @RequestMapping(value = "/signinListByUser")
    @ResponseBody
    public Result signinListByUser( OwSigninDto owSigninDto,PageDto pageDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwSigninDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            //插入数据
            if(StringUtils.isNull(owSigninDto.getA2userId())){
                owSigninDto.setA2userId(userId);
            }

            pageDto = PageUtils.resetPageDto(pageDto);

            //调用远程服务
            result =  owSigninService.pagination(owSigninDto,pageDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    @RequestMapping(value = "/addOffpostRecord")
    @ResponseBody
    public Result addOffpostRecord( OwOffpostRecordDto owOffpostRecordDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwOffpostRecordDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            owOffpostRecordDto.setCreatemanId(userId);
            owOffpostRecordDto.setCreateman(username);
            owOffpostRecordDto.setCompanyId(companyId);
            owOffpostRecordDto.setA2userId(userId);


            //调用远程服务
            result =  owOffpostRecordService.addData(owOffpostRecordDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    @RequestMapping(value = "/queryOffpostRecordList")
    @ResponseBody
    public Result queryOffpostRecordList( OwOffpostRecordDto owOffpostRecordDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwOffpostRecordDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //查询参数
            String userId = WebUtils.getUserId(session);
            owOffpostRecordDto.setCreatemanId(userId);

            //调用远程服务
            result =  owOffpostRecordService.pagination(owOffpostRecordDto,null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }









}
